--
-- Author: shunguo.chen
-- Date: 2017-09-11 11:56:12
--
-- ZoneListRender	服务器目录

local FriendZoneListRender = app.mvc.createListViewCell(...)
local HeadRender = require("app.widget.renders.HeadRender")

--data = {tag=1, name="1-10"}
function FriendZoneListRender.create(node)
	local sprite = FriendZoneListRender.extend(node or cc.CSLoader:createNode(Res.LoginZoneNode3))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function FriendZoneListRender:init(data)
	self.super.init(self, data)
	self:setContentSize( self._ui.Panel:getContentSize() )

	self._headRender = HeadRender.create(self._ui["Panel/head"], 3)
	self._ui["Panel/icon3"]:setVisible(false)
	self._ui["Panel/combat_num"]:setVisible(false)

	self._ui["Panel/icon4"]:setVisible(false)
	self._ui["Panel/time"]:setVisible(false)
	self._ui["Panel/btn_go"]:addClickEventListener(function()
		if self._cellModel then
			self._cellModel:dispatchEvent("select", self._data.serverInfo)
		end
	end)
	self:refresh()
end

function FriendZoneListRender:refresh()
	if self._data then
		self:setHead(self._data)
		local name = self._data.name.."("..self._data.nickName..")"
		self._ui["Panel/Text_name"]:setString(self:_convertName(name))
		self._ui["Panel/lv_num"]:setString(self._data.level)
		self._ui["Panel/text_server"]:setString(self._data.serverInfo.name)
	end
end

function FriendZoneListRender:setHead(data)
	if data.headUrl then
		self._headRender:setAvatarIconFromUrl(data.headUrl, app.channelAPI:getHeadCachePathByOpenId(data.openId))
		-- 这里 服务器好像没传 hairId 不能				
		self._headRender:setData({classID = tonumber(data.class), level =tonumber(data.level), raceID = tonumber(data.race or 1), gender = tonumber(data.gender or 1)})
	end
	
end

function FriendZoneListRender:onBtnClick(cb)
	self._onBtnClick = cb
end

function FriendZoneListRender:_convertName(name)
	local len = string.utf8len(name)
	if len > 14 then
		return string.usub(name, 1, 14-1) .. "..."
	else
		return name
	end
end
	

return FriendZoneListRender